package com.xcity.game.combat.skill.impl;

import java.util.List;

import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.ActionEffect.Value;
import com.xcity.game.combat.Round;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.buff.CombatBuff;
import com.xcity.game.combat.skill.CombatSkill;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.skill.Skill;

/**
 * 谈笑风生<br/>
 * 恢复我方全体相当于{0=50#0}%技能伤害的气势值，并给随机一名队友套上等同于自己气势值{1=50#0}%的护盾
 * @author yang.li
 *
 */
public class COMBAT_SKILL_90085 extends CombatSkill {
	
	public COMBAT_SKILL_90085(Skill skill) {
		super(skill);
	}
	
	@Override
	public void calcDamage(Action action) {
		float rate = skill.getParameter(0) / GameStaticConfig.percentBase;
		CombatUnit source = action.unit;
		int val = (int) (source.getMagicPower() * rate);
		val = Math.max(val, 1);
		List<CombatUnit> units = source.getTeam().all();
		for (int i = 0; i < units.size(); i++) {
			CombatUnit u = units.get(i);
			if (!u.isDied()) {
				// 加血
				ActionEffect effect = action.addTarget(u);
				u = effect.getUnit(); // 修正目标
				effect.effect(CombatAttributeType.HP, val);
			}
		}
		// 加buff
		CombatUnit u = source.getTeam().getUnitRandomly();
		if (u != null) {
			int hp = (int) (source.getHp() * skill.getParameter(1) / GameStaticConfig.percentBase);
			if (hp > 0) {
				action.addBuff(new BUFF(skill, u, hp));
			}
		}
	}
	
	static class BUFF extends CombatBuff {
		int hp; // 护盾（额外血量）
		public BUFF(Skill skill, CombatUnit owner, int hp) {
			super(skill.getTemplate().getCombatBuff(), owner);
			this.hp = hp;
		}
		@Override
		public void preDamage(ActionEffect effect) {
			active = true;
			int delta = effect.getHpDelta();
			if (delta < 0 && hp > 0) {
				hp += delta;
				Value v = effect.getValue(CombatAttributeType.HP);
				v.setDelta(hp > 0 ? 0 : hp);
			}
		}
		@Override
		public void roundEnd(Round round) {
			if (hp <= 0 && state == STATE_ADDED) { // 破盾
				uneffect();
				round.addBuff(this);
			}
		}
	}

}
